﻿package com.badibadi.fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import com.badibadi.activity.ClubListActivity;
import com.badibadi.activity.CreationNewClubActivity;
import com.badibadi.activity.IndexActivity;
import com.badibadi.infos.AddingRecordNewInfos;
import com.badibadi.infos.ClueTypeModel;
import com.badibadi.infos.Results;
import com.badibadi.infos.listType_Model;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.TempTools;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 查询界面的俱乐部
 * 
 * @author Administrator
 * 
 */
public class IndexClub0Fragment extends BaseFragment {

	private GridView gv;
	private IndexGridViewAdapter indexGridViewadapter;
	private Button index_creationclub;// 创建俱乐部按钮
	private MySharePreferences preferences;
	private int languageType = 0;
	private List<ClueTypeModel> clueTypeModels;
	String languageType_string;
	private listType_Model listType;
	/**
	 * 存储颜色
	 */
	private int[] color_id;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(getActivity());
				try {
					Utils.showMessage(getActivity(), getResources()
							.getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:// 数据下载完毕
				try {
					Utils.ExitPrgress(getActivity());
					indexGridViewadapter = new IndexGridViewAdapter(getActivity());
					gv.setAdapter(indexGridViewadapter);
				} catch (Exception e) {
					// TODO: handle exception
				}
		

				break;

			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		AddingRecordNewInfos arnInfos = new AddingRecordNewInfos(getActivity());
		languageType_string=Dialog.getSystemLanguageType(getActivity());
		color_id = new int[] {
				getActivity().getResources().getColor(R.color.orange_fa7822),
				getActivity().getResources().getColor(R.color.blue),
//				getActivity().getResources().getColor(),
				getActivity().getResources().getColor(R.color.black_50000000),
				getActivity().getResources().getColor(R.color.black_0) };
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_index_club0, null);
		preferences = new MySharePreferences(getActivity(), "language");
		languageType = preferences.get("language", 0);
		listType = new listType_Model();

		view.findViewById(R.id.index_creationclub).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						if (!Utils.getUid(getActivity()).equals("")) {
						
						Intent intent = new Intent(getActivity(),
								CreationNewClubActivity.class);
						startActivity(intent);
						} else {
							Toast.makeText(getActivity(),
									getResources().getString(R.string.l_xb10), 0)
									.show();
						}
					}
				});
		gv = (GridView) view.findViewById(R.id.index_club_gridview);
		// gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> arg0, View arg1,
		// int position, long arg3) {
		// // TODO Auto-generated method stub
		// // Toast.makeText(getActivity(),
		// // "选择了" + clueTypeModels.get(position).getName(), 0)
		// // .show();
		// Intent intent = new Intent(getActivity(),
		// ClubListActivity.class);
		// intent.putExtra("ClubListActivity",
		// clueTypeModels.get(position));
		// startActivity(intent);
		// }
		// });
		Ahow_Type_Clue();
		return view;
	}

	private void Ahow_Type_Clue() {
		Utils.showPrgress(getActivity());
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", "julebu");
				map.put("languageType", languageType_string);
				String cluecontent = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "type/listType");
				if (cluecontent != null) {

					Results results = Utils.checkResult_NNN(getActivity(),
							cluecontent);
//					   
					if (results != null && results.getRetmsg() != null) {
						try {
							//    (results.getRetmsg());
							listType= JSONUtils.getEntityByJsonString(
									results.getRetmsg(), listType_Model.class);
							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {

					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	public class IndexGridViewAdapter extends BaseAdapter {

		private Context context;

		// private String[] project;
		// private MyClubInfos myClubInfos;

		public IndexGridViewAdapter(Context ct) {
			context = ct;
			// project = context.getResources().getStringArray(
			// R.array.activity_type);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listType.getLists().size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listType.getLists().get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
//			LayoutInflater inflater=LayoutInflater.from(context);
//			View view=inflater.inflate(R.layout.only_one_btn, null);
			Button btn =new Button(getActivity());
			btn.setId(position);
			btn.setTextColor(color_id[position % color_id.length]);//
			// android:duplicateParentState="true"
			btn.setFocusable(false);
			// btn.setDuplicateParentStateEnabled(true);
			btn.setSingleLine(true);
			btn.setMaxEms(8);
			btn.setEllipsize(TruncateAt.END);
			
			//    
			// "position%color_id.length--->"+position%color_id.length);
			btn.setText(listType.getLists().get(position).getName());
			btn.setBackgroundResource(R.drawable.shape_graye5e5e5_circular_bead);
			btn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(getActivity(),
							ClubListActivity.class);
					clueTypeModels=new ArrayList<ClueTypeModel>();
					for(int i=0;i<listType.getLists().size();i++)
					{
						ClueTypeModel cc=new ClueTypeModel();
						cc.setId(listType.getLists().get(i).getId());
						cc.setName(listType.getLists().get(i).getName());
						clueTypeModels.add(cc);
					}
					intent.putExtra("ClubListActivity",
							clueTypeModels.get(position));
					startActivity(intent);
				}
			});
			return btn;
		}

	}

}
